import {
	app,
	BrowserWindow,
	ipcMain, //监听渲染进程事件
} from 'electron'
import lzupdate from '../renderer/utils/function/update';
import version from '../renderer/utils/function/version';
import setUpWindow from '../renderer/utils/function/subWindow';//创建设置的子窗口
import databaseWindow from '../renderer/utils/function/databaseWindow';//创建数据库的子窗口
if (process.env.NODE_ENV !== 'development') {
	global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}
let mainWindow
const winURL = process.env.NODE_ENV === 'development' ?
	`http://localhost:9090` :
	`file://${__dirname}/index.html`

function createWindow() {
	//初始化窗口
	mainWindow = new BrowserWindow({
		height: 900,
		width: 1400,
		useContentSize: true,
		icon: '.\\build\\icons\\256.png',
		title: '李钊模板',
		frame: true,
		webPreferences: {
			nodeIntegration: true,
			webSecurity: false
		},
	})
	// 加载远程URL或本地html
	mainWindow.loadURL(winURL)

	//关闭窗口
	mainWindow.on('closed', () => {
		mainWindow = null
	})
	//检测更新
	lzupdate.updateHandle(mainWindow,"http://117.50.31.205:8099/electron/")
	//创建设置子窗口
	setUpWindow.newWindow(mainWindow,winURL)
	//创建数据库子窗口
	databaseWindow.newWindow(mainWindow,winURL)
}
ipcMain.on("sendMessage", (event, data) => {
	console.log(data);
	if (data.action == 'min-app') { //最小化窗口
		// 最小化
		mainWindow.minimize();
	}
	if (data.action == 'close-app') { //关闭窗口
		if (data.isLogin) { //是否登录成功进入首页
			// 关闭窗口
			// mainWindow.close();
		} else {
			// 关闭窗口
			mainWindow.close();
		}
	}
	if(data.action == 'childMsg'){//接收子窗口发送的数据
		mainWindow&&mainWindow.send('emitMsg',data.data)//向渲染界面发送数据
	}
})
app.on('ready', createWindow)

app.on('window-all-closed', () => {
	if (process.platform !== 'darwin') {
		app.quit()
	}
})
app.on('activate', () => {
	if (mainWindow === null) {
		createWindow()
	}
})